package com.luo.chengrui.module.sa.controller.admin.role;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.role.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.role.RoleDO;
import com.luo.chengrui.module.sa.convert.role.RoleConvert;
import com.luo.chengrui.module.sa.service.role.RoleService;

@Tag(name = "管理后台 - 角色 ")
@RestController
@RequestMapping("/sa/role")
@Validated
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/create")
    @Operation(summary = "创建角色 ")
    @PreAuthorize("@ss.hasPermission('sa:role:create')")
    public CommonResult<String> createRole(@Valid @RequestBody RoleCreateReqVO createReqVO) {
        return success(roleService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色 ")
    @PreAuthorize("@ss.hasPermission('sa:role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody RoleUpdateReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:role:delete')")
    public CommonResult<Boolean> deleteRole(@RequestParam("id") String id) {
        roleService.deleteRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:role:query')")
    public CommonResult<RoleRespVO> getRole(@RequestParam("id") String id) {
        RoleDO role = roleService.getRole(id);
        return success(RoleConvert.INSTANCE.convert(role));
    }

    @GetMapping("/list")
    @Operation(summary = "获得角色 列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:role:query')")
    public CommonResult<List<RoleRespVO>> getRoleList(@RequestParam("ids") Collection<String> ids) {
        List<RoleDO> list = roleService.getRoleList(ids);
        return success(RoleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色 分页")
    @PreAuthorize("@ss.hasPermission('sa:role:query')")
    public CommonResult<PageResult<RoleRespVO>> getRolePage(@Valid RolePageReqVO pageVO) {
        PageResult<RoleDO> pageResult = roleService.getRolePage(pageVO);
        return success(RoleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色  Excel")
    @PreAuthorize("@ss.hasPermission('sa:role:export')")
    @OperateLog(type = EXPORT)
    public void exportRoleExcel(@Valid RoleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RoleDO> list = roleService.getRoleList(exportReqVO);
        // 导出 Excel
        List<RoleExcelVO> datas = RoleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "角色 .xls", "数据", RoleExcelVO.class, datas);
    }



}
